package com.it.myh.edls;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/*
* e-dls算法相对于dls算法而言，仅考虑了物理距离这一因素
* */
public class EnhancedDLSAlgorithm {
    private static final double MAX_DISTANCE = 1.0; // 最大距离阈值，超过该距离将被替换为假位置
    private static final int MAX_DUMMY_LOCATIONS = 3; // 生成的假位置数量

    public static void main(String[] args) {
        // 原始位置列表
        List<Location> originalLocations = new ArrayList<>();
        originalLocations.add(new Location(40.7128, -74.0060)); // 纽约
        originalLocations.add(new Location(34.0522, -118.2437)); // 洛杉矶
        originalLocations.add(new Location(51.5074, -0.1278)); // 伦敦

        // 生成假位置
        List<Location> dummyLocations = generateDummyLocations(originalLocations);

        // 输出原始位置和假位置
        System.out.println("原始位置：");
        printLocations(originalLocations);

        System.out.println("假位置：");
        printLocations(dummyLocations);
    }

    private static List<Location> generateDummyLocations(List<Location> originalLocations) {
        List<Location> dummyLocations = new ArrayList<>();

        Random random = new Random();
        for (int i = 0; i < MAX_DUMMY_LOCATIONS; i++) {
            double lat = random.nextDouble() * 180 - 90; // 随机生成纬度
            double lon = random.nextDouble() * 360 - 180; // 随机生成经度

            Location dummyLocation = new Location(lat, lon);

            // 检查生成的假位置与原始位置的距离，如果距离小于等于阈值，重新生成
            while (isCloseToOriginalLocations(dummyLocation, originalLocations) || isCloseToDummyLocations(dummyLocation, dummyLocations)) {
                lat = random.nextDouble() * 180 - 90;
                lon = random.nextDouble() * 360 - 180;
                dummyLocation = new Location(lat, lon);
            }

            dummyLocations.add(dummyLocation);
        }

        return dummyLocations;
    }

    private static boolean isCloseToOriginalLocations(Location dummyLocation, List<Location> originalLocations) {
        for (Location originalLocation : originalLocations) {
            double distance = calculateDistance(dummyLocation, originalLocation);
            if (distance <= MAX_DISTANCE) {
                return true;
            }
        }
        return false;
    }

    private static boolean isCloseToDummyLocations(Location dummyLocation, List<Location> dummyLocations) {
        for (Location location : dummyLocations) {
            double distance = calculateDistance(dummyLocation, location);
            if (distance <= MAX_DISTANCE) {
                return true;
            }
        }
        return false;
    }

    private static double calculateDistance(Location location1, Location location2) {
        // 计算两个位置之间的距离，可以使用球面三角法或其他距离计算公式
        double lat1 = Math.toRadians(location1.getLatitude());
        double lon1 = Math.toRadians(location1.getLongitude());
        double lat2 = Math.toRadians(location2.getLatitude());
        double lon2 = Math.toRadians(location2.getLongitude());

        double dlon = lon2 - lon1;
        double dlat = lat2 - lat1;

        double a = Math.sin(dlat / 2) * Math.sin(dlat / 2)
                + Math.cos(lat1) * Math.cos(lat2)
                * Math.sin(dlon / 2) * Math.sin(dlon / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        // 地球半径，单位为千米
        double radius = 6371;

        return radius * c;
    }

    private static void printLocations(List<Location> locations) {
        for (Location location : locations) {
            System.out.println(location.getLatitude() + ", " + location.getLongitude());
        }
    }
}

// 位置类
class Location {
    private double latitude;
    private double longitude;

    public Location(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }
}

